/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.krArmor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import ru.jSkyRoom.manager.JSkyGlobal;
import ru.jSkyRoom.manager.ModImporter;
import ru.jSkyRoom.manager.ModManager;
import ru.jSkyRoom.factory.AutoSelectionItemsGeneratorAdapterFactory;
import ru.jSkyRoom.factory.SelectorByEdidFactory;
import ru.jSkyRoom.generator.SimpleItemsGenerator;
import ru.jSkyRoom.generator.adapter.AutoSelectionItemsGenerator;
import ru.jSkyRoom.generator.command.rule.Rule;
import ru.jSkyRoom.interfaces.major.Armor;
import ru.jSkyRoom.interfaces.JSkyMod;
import ru.jSkyRoom.interfaces.major.Enchantment;
import ru.jSkyRoom.interfaces.major.Keyword;
import ru.jSkyRoom.matcher.builder.major.ArmorMatcherBuilder;
import ru.jSkyRoom.matcher.builder.major.KeywordMatcherBuilder;
import ru.jSkyRoom.selector.SelectorByEdid;
import ru.jSkyRoom.support.RegexFactory;
import ru.rakev.matcher.Matcher;
import ru.rakev.matcher.factory.MatcherAdapterFactory;
import ru.rakev.matcher.factory.MatcherDecoratorFactory;

/**
 *
 * @author Rakev
 */
public class EnchantmentGeneratorBuilder {

    private Collection<String> enchantmentEdidRegexs = new ArrayList<>();
    private Map<String, Matcher<Armor>> enchantmentEdidRegexToPermitedKeywordRegexLink = new HashMap<>();
    private Map<String, Matcher<Armor>> enchantmentEdidRegexToForbidenKeywordRegexLink = new HashMap<>();
    private Map<String, String> enchantmentEdidRegexToNameAppendixLink = new HashMap<>();
    private Collection<String> enchantmentEdidRegexToLevelingName = new ArrayList();

    public void addEnchantmentEdidRegex(String... edidRegexs) {
        this.enchantmentEdidRegexs.addAll(Arrays.asList(edidRegexs));
    }

    public void addEnchantmentEdidRegexToPermitedKeywordRegexLink(String enchantmentEdidRegex, String... keywordEdidRegexs) {
        String totalRegex = new RegexFactory().anyOf(keywordEdidRegexs);
        Matcher<Armor> newMatcher = createKeywordMatcher(totalRegex);
        Matcher<Armor> oldMatcher = enchantmentEdidRegexToPermitedKeywordRegexLink.get(enchantmentEdidRegex);
        if (oldMatcher != null) {
            newMatcher = MatcherDecoratorFactory.create().anyOf(oldMatcher, newMatcher);
        }
        this.enchantmentEdidRegexToPermitedKeywordRegexLink.put(enchantmentEdidRegex, newMatcher);
    }

    public void addEnchantmentEdidRegexToForbidenKeywordRegexLink(String enchantmentEdidRegex, String... keywordEdidRegexs) {
        String totalRegex = new RegexFactory().allOf(keywordEdidRegexs);
        Matcher<Armor> newMatcher = createKeywordMatcher(totalRegex);
        Matcher<Armor> oldMatcher = enchantmentEdidRegexToForbidenKeywordRegexLink.get(enchantmentEdidRegex);
        if (oldMatcher != null) {
            newMatcher = MatcherDecoratorFactory.create().allOf(oldMatcher, newMatcher);
        }
        this.enchantmentEdidRegexToForbidenKeywordRegexLink.put(enchantmentEdidRegex, newMatcher);
    }

    public void addEnchantmentEdidRegexToNameAppendixLink(String edidRegex, String appendix) {
        this.enchantmentEdidRegexToNameAppendixLink.put(edidRegex, appendix);
    }

    public void addEnchantmentEdidRegexToNameAppendixLink(Map<String, String> edidRegexToNameAppendixLink) {
        this.enchantmentEdidRegexToNameAppendixLink.putAll(edidRegexToNameAppendixLink);
    }

    public void addEnchantmentEdidRegexToLevelingName(String... edidRegexs) {
        this.enchantmentEdidRegexToLevelingName.addAll(Arrays.asList(edidRegexs));
    }

    public AutoSelectionItemsGenerator<Armor> build() {
        Collection<Enchantment> aviableEnchantments = selectAviableEnchantment();
        Collection<Rule<Armor>> armorGenerationRules = new ArrayList<>(aviableEnchantments.size());

        for (Enchantment enchantment : aviableEnchantments) {
            EnchanterRuleCreator enchanterRuleCreator = new EnchanterRuleCreator(getCreatingMod(), enchantment, enchantmentEdidRegexToPermitedKeywordRegexLink, enchantmentEdidRegexToForbidenKeywordRegexLink, enchantmentEdidRegexToNameAppendixLink, enchantmentEdidRegexToLevelingName);
            Rule<Armor> armorGenerationRule = enchanterRuleCreator.create();
            armorGenerationRules.add(armorGenerationRule);
        }
        SimpleItemsGenerator itemsGenerator = new SimpleItemsGenerator<>(armorGenerationRules);
        return AutoSelectionItemsGeneratorAdapterFactory.create().adaptionArmorGenerator(itemsGenerator);

    }

    private Collection<Enchantment> selectAviableEnchantment() {
        SelectorByEdid<Enchantment> selector = SelectorByEdidFactory.create().createEnchantmentSelector();
        Collection<Enchantment> selected = selector.select(enchantmentEdidRegexs);
        return selected;
    }

    private Matcher<Armor> createKeywordMatcher(String keywordEdidRegex) {
        Matcher<Keyword> edidKeywordMatcher = KeywordMatcherBuilder.create().edid().matches(keywordEdidRegex);
        Matcher<Armor> setMatcher = ArmorMatcherBuilder.create().keywords().anyOf(edidKeywordMatcher);

        return setMatcher;
    }

    private JSkyMod getCreatingMod() {
        return ModManager.getInstance().getActiveMod();
    }
}
